<template>
  <div>
    <el-dialog title="分配角色" :visible.sync="disDialogvisible" width="50%" @close="disDialogClose">
      <!-- 内容主题区域 -->
      <el-form label-width="90px" ref="disFormref">
        <el-row>
          <el-col>
            <el-form-item label="当前用户:">
              <el-input v-model="disshow.username" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col>
            <el-form-item label="当前角色:">
              <el-input v-model="disshow.role_name"></el-input>
            </el-form-item>
          </el-col>
          <el-col>
            <el-form-item label="分配新角色:">
              <el-select v-model="selectuid" placeholder="请选择">
                <el-option v-for="item in roleList" :key="item.id" :label="item.roleName" :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <!-- 底部区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="disDialogClose">取 消</el-button>
        <el-button type="primary" @click="disuserinfo">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 分配角色显示
      disDialogvisible: false,
      // 分配角色数据
      disshow: {},
      // 分配角色列表
      roleList: [],
      selectuid: ''
    }
  },
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    role: {
      type: Object
    },
    getUserList: {
      type: Function,
      default: null
    }
  },
  watch: {
    visible (val, oldval) {
      this.disDialogvisible = val
    },
    // 获取父级传过来的数据  并获取配置列表
    role (val, oldval) {
      this.disshow = val
      this.disuserfp()
    }
  },
  methods: {
    // 获取分配角色列表
    disuserfp (role) {
      this.$http.get('http://127.0.0.1:8888/api/private/v1/roles')
        .then(res => {
          if (res.data.meta.status !== 200) return this.$message.error(res.data.meta.msg)
          // 数据接收
          this.roleList = res.data.data
          // console.log(this.roleList)
        })
        .catch(error => {
          console.log(error)
        })
      this.disDialogvisible = true
    },
    // 上传分配角色
    disuserinfo () {
      if (!this.selectuid) return this.$message.error('请选择角色！')
      this.$http.put(`users/${this.disshow.id}/role`, {
        rid: this.selectuid
      })
        .then(res => {
          if (res.data.meta.status !== 200) return this.$message.error(res.data.meta.msg)
          // 数据接收
          this.$message.success(res.data.meta.msg)
          this.getUserList()
          this.disDialogClose()
        })
        .catch(error => {
          console.log(error)
        })
    },
    // 监听用户修改对话框的关闭事件
    disDialogClose () {
      this.$emit('update:visible', false)
      this.$refs.disFormref.resetFields()
      this.selectuid = ''
    }
  }
}
</script>
<style>

</style>
